﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using WebTV_Kickstarter;

namespace Kickstarter.Controllers
{
    public class MessageController : Controller
    {
        // GET: /Message/
        public ActionResult Index()
        {
            if (Session["Gebruiker"] != null)
            {
                return View(Message.Index());          
            }
            else
            {
                return RedirectToAction("../Home/Index");
            }
        }

        public ActionResult Thankyou(int? id)
        {
            ViewBag.ProjectID = id;
            return View();
        }

        // GET: /Message/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Message message = Message.FindMessage(id);
            if (message == null)
            {
                return HttpNotFound();
            }
            return View(message);
        }

        // GET: /Message/Create
        public ActionResult Create(int? id)
        {
            
                if (id == null)
                {
                    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
                }
                if (Session["Gebruiker"] != null)
                {
                    Project project = Project.FindProject(id);
                    if (project == null)
                    {
                        return HttpNotFound();
                    }
                    Gebruiker g = (Gebruiker)Session["Gebruiker"];
                    ViewBag.ProjectName = project.Title;
                    ViewBag.UserName = Session["Gebruiker"] == null ? String.Empty : (string)g.Name;
                    Message m = new Message();
                    m.ProjectId = (int)id;
                    m.GebruikerId = g.Id;
                    return View(m);
                }
                else
                {
                    Session["redirectBack"] = "../Message/Create/" + id;
                    return RedirectToAction("../Gebruiker/Login");
                }
            
        }

        // POST: /Message/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.

        // Moet nog bij komen dat het totale aantal messages bij een project worden teruggegeven.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include="Id,Context,ProjectId,GebruikerId")] Message message)
        {
            if (ModelState.IsValid && message.Context != null)
            {
                Message.AddMessage(message);

                return RedirectToAction("Thankyou/" + message.ProjectId);
            }
            ViewBag.ProjectId = new SelectList(Message.GetProjectSet(), "Id", "Title", message.ProjectId);
            ViewBag.GebruikerId = new SelectList(Message.GetGebruikerSet(), "Id", "Name", message.GebruikerId);
            return View(message);
        }
    }
}
